<?php
/**
 * 后台会员组管理
 * @author yangzhiguo
 */
defined('IN_LK') or die('Access Denied');
lk::lo_app('member_common', 'member', 0);
class member_group extends member_common
{
	
	/**
	 * @param resource $db
	 */
	private $group_db;
	
	public function __construct()
	{
		parent::__construct();
		$this->group_db = lk::load_model('member_group');
	}
	
	/**
	 * 默认方法
	 */
	public function initialize()
	{
		
	}
	
	/**
	 * 会员组管理
	 */
	public function manage()
	{
		$grouplist = $this->get_group();
		include self::admin_t('member_group_list');
	}
	
	/**
	 * 会员组排序
	 */
	public function listorder()
	{
		if(isset($_POST['dosubmit']))
		{
			$listorder = $_POST['listorder'];
			foreach($listorder as $groupid => $order)
			{
				$groupid = (int)$groupid;
				$order   = (int)$order;
				$this->group_db->update(array('listorder' => $order), "`groupid`='$groupid'");
			}
			$this->update_group();
			showmsg(LANG('update_success'), true);
		}
	}
	
	/**
	 * 编辑会员组
	 */
	public function edit()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = isset($_POST['info']) ? $_POST['info'] : array();
			if(!isset($info['allowpost']))       : $info['allowpost']       = 0; endif;
			if(!isset($info['allowmessage']))    : $info['allowmessage']    = 0; endif;
			if(!isset($info['allowattachment'])) : $info['allowattachment'] = 0; endif;
			if(!isset($info['allowsearch']))     : $info['allowsearch']     = 0; endif;
			$data = in_fields($info, array('groupid', 'name', 'point', 'allowpost', 
											'allowsearch', 'allowmessage', 
											'allowattachment', 'maxposts', 
											'maxmessages', 'description'));
			if($this->checkname_ajax($data['name'], $info['groupid']))
			{
				showmsg(LANG('groupname_exists'), false);
			}
			$where = "`groupid`='" . $data['groupid'] . "'";
			unset($data['groupid']);
			$this->group_db->update($data, $where);
			$this->update_group();
			showmsg(LANG('update_success'), '', '', '', 'edit');
		}
		$groupid = isset($_GET['groupid']) ? (int)$_GET['groupid'] : 0;
		if(!$groupid)
		{
			showmsg(LANG('parameters_error'), false);
		}
		$info = $this->group_db->get_one('*', "`groupid`='$groupid'");
		$ac = 'edit';
		include self::admin_t('member_group_add');
	}
	
	/**
	 * 添加会员组
	 */
	public function add()
	{
		if(isset($_POST['dosubmit']))
		{
			$info = isset($_POST['info']) ? $_POST['info'] : array();
			$data = in_fields($info, array('name', 'point', 'allowpost', 'allowsearch', 'allowmessage', 'allowattachment', 'maxposts', 'maxmessages', 'description'));
			if($this->checkname_ajax($data['name']))
			{
				showmsg(LANG('groupname_exists'), false);
			}
			if($this->group_db->insert($data))
			{
				$this->update_group();
				showmsg(LANG('update_success'), true, '', '', 'add_member_group');
			}
		}
		else
		{
			include self::admin_t('member_group_add');
		}
	}
	
	/**
	 * 检测会员组名称是否存在
	 * @param string $groupname 非空返回 空ajax
	 * @param int    $not  不包含的groupid
	 * @return 存在输出1 不存在输出0
	 */
	public function checkname_ajax($groupname = '', $not = 0)
	{
		if($groupname)
		{
			$name  = $groupname;
			$where = $not ? "AND `groupid`!='$not'" : '';
		}
		else
		{
			$name = isset($_GET['name']) ? $_GET['name'] : '';
			$where= isset($_GET['not'])  ? " AND `groupid`!='" . (int)$_GET['not'] . "'" : "";
		}
		$r = $this->group_db->get_one('name', "`name`='$name'" . $where);
		if($groupname)
		{
			return (boolean)$r;
		}
		$r ? exit('0') : exit('1');
	}
	
	/**
	 * 更新会员组缓存
	 * @return true
	 */
	private function update_group()
	{
		$grouplist = $this->group_db->get('*', '', '`listorder`,`groupid` ASC');
		set_cache('grouplist', $grouplist);
		return true;
	}
	
	/**
	 * 读取会员组缓存
	 * @return array $r
	 */
	private function get_group()
	{
		$r = get_cache('grouplist');
		if(!$r)
		{
			$this->update_group();
			$r = get_cache('grouplist');
		}
		return $r;
	}
}